package com.tebiecloud.order.server.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.mpweixin.client.constants.QueueConstants;
import com.tebiecloud.order.client.model.entity.Reserve;
import com.tebiecloud.order.client.model.vo.ReserveList;
import com.tebiecloud.order.server.service.ReserveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Payload;


/**
 * 监听排号事件
 */
@Slf4j
@Configuration
public class ReserveEventInfoListener {

    @Autowired
    ReserveService reserveService;

    @RabbitListener(queues = QueueConstants.QUEUE_RESERVE_EVENT_INFO)
    public void ReserveQueue(@Payload Reserve reserve){
        try {
            long currentTime = System.currentTimeMillis();
            long reserveTime = reserve.getUt().getTime();
            long sleepTime = 0;
            if(1000*60*10>(currentTime-reserveTime)){
                sleepTime = 1000*60*10 - (currentTime-reserveTime);
                log.info("下一个应该睡眠{}毫秒",sleepTime);
                Thread.sleep(sleepTime);
            }
            log.info(">>minjie 延迟{}毫秒接收处理排号单 reserveId:{}",sleepTime,reserve.getReserveId());
            //查询排号
            Reserve reserveOld = reserveService.getById(reserve.getReserveId());
            if(reserveOld!=null && reserveOld.getStatus().equals(2)){
                //更新排号状态
                Reserve newReserve = new ReserveList();
                newReserve.setReserveId(reserve.getReserveId());
                newReserve.setStatus(6);
                if(reserveService.updateById(newReserve)){
                    reserveService.noticeReserve(reserve.getStoreId());
                }
            }
        }catch (Exception e){
            log.info(e.getLocalizedMessage());
        }
    }
}
